package cn.dansj.handler.interfaces;

import cn.dansj.annotation.InterfaceParam;
import cn.dansj.annotation.MarkAsApi;
import cn.dansj.common.utils.bean.CacheToken;
import cn.dansj.common.utils.bean.Return;
import cn.dansj.common.utils.enums.ResponseCode;
import cn.dansj.common.utils.transfer.GetTime;
import cn.dansj.common.utils.transfer.Transformation;
import cn.dansj.entity.User;
import cn.dansj.enums.GlobalEnv;
import cn.dansj.exception.InterfaceResponseException;
import cn.dansj.handler.InterfaceHandler;
import cn.dansj.utils.project.ProjectUtils;
import com.alibaba.fastjson2.JSONObject;

import java.util.Map;

@MarkAsApi
public class EdgeLogin extends InterfaceHandler {
    @InterfaceParam(value = "username", require = true, comment = "用户名")
    @InterfaceParam(value = "password", require = true, comment = "密码")
    @Override
    public Object execute(Map<String, Object> paramsMap) {
        String username = paramsMap.get("username").toString();
        String password = paramsMap.get("password").toString();
        try {
            String newPwd = ProjectUtils.decryptByPrivateKey(password);
            Return<User> aReturn = ProjectUtils.checkLoginfo(username, newPwd);
            if (aReturn.getStatus()) {
                User user = aReturn.getData();
                CacheToken cacheToken = ProjectUtils.createToken(user);
                GlobalEnv.threadPool.execute(() -> {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    String key = GlobalEnv.CacheTokenPrefix + cacheToken.getUuid();
                    redisUtils.hset(key, "expireTime", "-1");
                    redisUtils.hset(key, "platform", "edge");
                    redisUtils.expire(key, -1);
                });
                JSONObject userInfo = new JSONObject();
                userInfo.put("username", user.getUsername());
                userInfo.put("nickname", user.getNickName());
                userInfo.put("avatar", user.getAvatar());
                userInfo.put("role", user.getRole());
                userInfo.put("token", cacheToken.getUuid());
                userInfo.put("mobile", user.getMobile());
                userInfo.put("mail", user.getMail());
                userInfo.put("sequence", user.getSequence());
                userInfo.put("crt_time", GetTime.stringTime(user.getCrtTime()));
                return ProjectUtils.encryptByPrivateKey(userInfo.toJSONString());
            }
            throw new InterfaceResponseException(ResponseCode.AuthenticationFailed.getStatusCode(), Transformation.nvl(aReturn.getMessage(), ResponseCode.AuthenticationFailed.getError()));
        } catch (Exception exception) {
            if (exception instanceof InterfaceResponseException) {
                throw exception;
            } else {
                throw new InterfaceResponseException(ResponseCode.AuthenticationFailed);
            }
        }
    }
}
